from flask import Flask, render_template, request
from ycdl import serivce
from datetime import timedelta

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1)


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/select", methods=['post'])
def select():
    # 接收参数
    # 基金编号
    code = request.form.get('code')
    # 开始日期
    start_date = request.form.get('startDate')
    # 结束日期
    end_date = request.form.get('endDate')

    # 投入金额
    money = float(request.form.get('money'))
    # 周期 0 按周 1 按月  2 按日 3 最优方式
    cycle = int(request.form.get('cycle'))
    # 时间
    time = int(request.form.get('time'))

    # 对比值
    compare_btn = request.form.get('compare')
    # 判断是对比还是查询收益  Ture 查询收益  false 对比收益
    is_query = False
    if compare_btn == None:
        is_query = True
    line = serivce.do(code, start_date, end_date, money, cycle, time, is_query)

    return render_template('show.html', chart=line.render_embed())


@app.route("/line")
def line():
    return render_template("line_chart.html")


if __name__ == '__main__':
    app.run()
